<!--
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-30 15:05:24
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-30 16:56:23
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise对象</title>
</head>
<body>
    <script>

        //打印
        // Promise是一个对象
        function setTime(interval,i){
           Promise.resolve(setTimeout( ()=>console.log(i), interval))
        }
        async function sleep(params) {
            for(let i=1;i<= params;i++){
                 let result = await setTime(1000*i,i)
            }
        //  console.log(result)
        }
        sleep(10).catch(err=>console.log(err));

    //     function *sleep(){
    //         yield 1;
    //     }

    //    let m = sleep()
    //    setTimeout(()=>{
    //       console.log(m.next());
    //    },1000)
    let list = [new Promise((res,rej)=>res(1)),setTimeout(new Promise((res, rej) => rej(Error)),50000),new Promise((res, rej) => res(3)), new Promise((res, rej) => res(4))];
    let data = Promise.all(list)
        console.log(data.then(res=>console.log(res)));
    let data1 = Promise.race(list)
        console.log(data1.then(res => console.log(res)));
    let allSettled = Promise.allSettled(list)
        console.log(allSettled.then(res => console.log(res)));
        // 只要有一个变成fulfilled就会返回成功。
    let any = Promise.any(list);
    console.log(any);
    //promise对象的resolve方法会把当前对象变成一个Promise;
    let kpi = Promise.resolve('mmg');
       kpi.then(s=>console.log(s))





    </script>
</body>
</html>